#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import List, Dict

import nonebot
import psutil
import platform
import os
import json

from nonebot import get_driver

global_config = get_driver().config
adminID = global_config.dict()["admin_id"]


def get_adminID() -> str:
    nonebot.logger.info(f"admin管理员账号：{adminID}")
    return adminID


def os_status() -> str:
    os_name = platform.system() + "-" + platform.machine()  # 获取系统类型
    if os_name == "Darwin":
        os_name = "MacOS"
    if os.path.exists('/.dockerenv'):
        os_name = os_name + "(docker)"
    return os_name


def cpu_status() -> float:
    return psutil.cpu_percent(interval=1)  # type: ignore


def per_cpu_status() -> List[float]:
    return psutil.cpu_percent(interval=1, percpu=True)  # type: ignore


def memory_percent() -> float:
    return psutil.virtual_memory().percent


def memory_absolute() -> List:  # 返回内存绝对占用
    total = psutil.virtual_memory().total / 1024 / 1024 / 1024
    used = psutil.virtual_memory().used / 1024 / 1024 / 1024
    return [used, total]


def disk_usage() -> List:  # 返回硬盘用量
    used = psutil.disk_usage("/").used / 1024 / 1024 / 1024
    total = psutil.disk_usage("/").total / 1024 / 1024 / 1024
    return [used, total]


def disk_usage_detail() -> Dict[str, psutil._common.sdiskusage]:
    disk_parts = psutil.disk_partitions()
    disk_usages = {
        d.mountpoint: psutil.disk_usage(d.mountpoint) for d in disk_parts
    }
    return disk_usages


def device_info() -> str:  # 返回模拟的设备
    device_filepath = global_config.dict()["path_cqhttp"] + "/device.json"

    with open(device_filepath, 'r') as r:
        json_data = json.load(r)

        protocol_num = json_data["protocol"]

    device_type_dict = {
        0: "iPad",
        1: "Android Phone",
        2: "Android Watch",
        3: "MacOS",
        4: "企点"
    }

    device_type = device_type_dict.get(protocol_num)

    return "#Device(模拟): " + device_type


if __name__ == "__main__":
    print(cpu_status())
    print(memory_percent())
    print(disk_usage_detail())
    print(f"Disk: {int(disk_usage()[0])}G/{int(disk_usage()[1])}G")
